﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;
using SharePointBrowser.Properties;
using System.Drawing.Imaging;

namespace SharePointBrowser
{
    public class SPItemItem : SPItem
    {
        public string ItemId { get; set; }
        public string ContentType { get; set; }

        public SPItemItem(XmlNode itemNode)
            : base(itemNode)
        {
            ItemId = itemNode.Attributes["ows_ID"].Value;
            Name = itemNode.Attributes["ows_Title"] != null
                ? itemNode.Attributes["ows_Title"].Value
                : itemNode.Attributes["ows_LinkFilename"] != null
                    ? itemNode.Attributes["ows_LinkFilename"].Value
                    : "(No name)";
            ContentType = itemNode.Attributes["ows_ContentType"] != null ? itemNode.Attributes["ows_ContentType"].Value : string.Empty;
            if (ContentType == "Folder")
            {
                Image = Resources.FOLDER;
            }
            else if (itemNode.Attributes["ows_DocIcon"] != null)
            {
                Image = (Bitmap)Resources.ResourceManager.GetObject("IC" + itemNode.Attributes["ows_DocIcon"].Value.ToUpper());
            }
        }

        public override void Paint(System.Drawing.Graphics graphics)
        {
            graphics.DrawLine(DrawingHelper.SEPARATOR_PEN, HPADDING, rectangle.Bottom, rectangle.Right - HPADDING, rectangle.Bottom);
            if (Selected)
            {
                Rectangle selectionRect = rectangle;
                selectionRect.Inflate(-HPADDING, -VPADDING);
                graphics.FillRectangle(DrawingHelper.SEL_BRUSH, selectionRect);
                graphics.DrawRectangle(DrawingHelper.SEL_BORDER_PEN, selectionRect);
            }
            if (Image != null)
            {
                Rectangle destRect = new Rectangle(location.X + HPADDING + 5, location.Y + VPADDING + 5, Image.Width, Image.Height);
                ImageAttributes imgAttr = new ImageAttributes();
                imgAttr.SetColorKey(Color.White, Color.White);
                graphics.DrawImage(Image, destRect, 0, 0, destRect.Width, destRect.Height, GraphicsUnit.Pixel, imgAttr);
            }
            int textX = location.X + HPADDING + 15 + 15;
            if (trimmedName == null)
            {
                trimmedName = getTrimmedName(graphics);
            }
            graphics.DrawString(trimmedName, DrawingHelper.DESCRIPTION_FONT, DrawingHelper.LABEL_BRUSH, textX, location.Y + VPADDING);
        }

        protected override string getTrimmedName(Graphics graphics)
        {
            int elipsisWidthName = (int)graphics.MeasureString(elipsis, DrawingHelper.DESCRIPTION_FONT).Width;
            int textMaxWidth = size.Width - (HPADDING + ImageWidth + 15 + 15) - HPADDING - elipsisWidthName - 10;
            return DrawingHelper.TrimText(Name, elipsis, textMaxWidth, graphics, DrawingHelper.DESCRIPTION_FONT);
        }
    }
}
